package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.limao.DBDataClass.SysStudyDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class SysStudyProvider {

	private static SysStudyProvider instance=null;

	public static SysStudyProvider getInstance() {
		if(instance==null)
		{
			instance=new SysStudyProvider();
		}
		return instance;
	}
	
	// 初始化
	private SysStudyProvider() {

	}
	
	
	//全部  有效的新手学堂资料
	public ArrayList<SysStudyDataClass> Load_SySNewStudy_All() {
		
		ArrayList<SysStudyDataClass> list = new ArrayList<SysStudyDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT studyid,studytitle,studycontent,studytype,studysort,createtime,updatetime FROM cd_sys_study where studybooks=0 and state=1 order by studysort desc");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				SysStudyDataClass obj = new SysStudyDataClass();
				obj.studyid = rs.getString("studyid");// ID
				obj.studytitle = rs.getString("studytitle") ;
				obj.studycontent = rs.getString("studycontent");
				obj.studytype = rs.getString("studytype");
				obj.studysort = rs.getString("studysort");
				obj.createtime = rs.getString("updatetime");
				if(CacheClass.isEmpty(obj.createtime))
				{
					obj.createtime = rs.getString("createtime");
				}
				
				list.add(obj);
			}
		} catch (Exception e) {
			list=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return list;

	}
	
	//全部  有效的  规则  资料
	public ArrayList<SysStudyDataClass> Load_SySNewRule_All() {
		
		ArrayList<SysStudyDataClass> list = new ArrayList<SysStudyDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT studyid,studytitle,studycontent,studytype,studysort,createtime,updatetime FROM cd_sys_study where studybooks=1 and state=1 order by studysort desc");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				SysStudyDataClass obj = new SysStudyDataClass();
				obj.studyid = rs.getString("studyid");// ID
				obj.studytitle = rs.getString("studytitle") ;
				obj.studycontent = rs.getString("studycontent");
				obj.studytype = rs.getString("studytype");
				obj.studysort = rs.getString("studysort");
				obj.createtime = rs.getString("updatetime");
				if(CacheClass.isEmpty(obj.createtime))
				{
					obj.createtime = rs.getString("createtime");
				}
				list.add(obj);
			}
		} catch (Exception e) {
			list=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return list;

	}
		
	public SysStudyDataClass Load_SySNewRule_ById(int studyid) {
		
		SysStudyDataClass tmp = new SysStudyDataClass();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT studyid,studytitle,studycontent,studytype,studysort,createtime,updatetime FROM cd_sys_study where studyid=?");
			pstmt.setInt(1, studyid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp.studyid = rs.getString("studyid");// ID
				tmp.studytitle = rs.getString("studytitle") ;
				tmp.studycontent = rs.getString("studycontent");
				tmp.studytype = rs.getString("studytype");
				tmp.studysort = rs.getString("studysort");
				tmp.createtime = rs.getString("createtime");
				if(CacheClass.isEmpty(tmp.createtime))
				{
					tmp.createtime = rs.getString("createtime");
				}
			}
		} catch (Exception e) {
			tmp=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;

	}
			
	
}
